package com.itheima.testdemo;

import java.util.List;

//抢红包的线程类
public class SnatchRedPacket extends Thread {

    private List<Integer> redPacket;

    public SnatchRedPacket(List<Integer> redPacket, String name) {
        super(name);
        this.redPacket = redPacket;
    }

    @Override
    public void run() {

        String name = Thread.currentThread().getName();

        while (true) {

            synchronized (redPacket) {

                if (redPacket.size() == 0) {
                    break;
                }

                int index = (int) (Math.random() * redPacket.size());
                int money = redPacket.remove(index);
                System.out.println(name + "抢到了" + money + "元");


                if (redPacket.size() == 0) {
                    System.out.println("红包已抢完！");
                    break;
                }
            }

            /*try {
                Thread.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }*/
        }

    }
}
